import COS from 'cos-js-sdk-v5'
import { teachCommonStsToken } from '../stores/api/live'
import { teachCommonStsToken2 } from '../stores/api/course'
import { message } from 'ant-design-vue';

// 图片上传
function upload(file,callback) {
    let fileUrl = ''
  teachCommonStsToken({}, 'post').then(response => { // 后台接口返回 密钥相关信息
    const data = response.data
    let startIndex = file.name.lastIndexOf('.')
    let urls = file.name.substring(startIndex);
    const [y, m, d] = [new Date().getFullYear(), String(new Date().getMonth() + 1).padStart(2, '0'), String(new Date().getDate()).padStart(2, '0')];
    let uploadFileName =  `images/console/${y}${m}${d}/${Date.now()}_${Math.random().toString(36).substr(2, 6)}${urls}` // Date.parse(new Date())
    var cos = new COS({
        getAuthorization: function(options, callback) {
          callback({
            TmpSecretId: data.credentials.tmpSecretId,
            TmpSecretKey: data.credentials.tmpSecretKey,
            XCosSecurityToken: data.credentials.sessionToken,
            StartTime: data.startTime,
            ExpiredTime: data.expiredTime,
            // expiration: data.expiration,
            // requestId: data.requestId,
            ScopeLimit: true,
          })
        }
      })
      console.log(uploadFileName,data,file, file.name,urls, 'uploadFileName')
      cos.uploadFile({
          Bucket: data.bucket, // 存储桶名称
          Region: data.region, // 地区
          Key: uploadFileName, // 图片名称
          Body: file, // 上传文件对象
          Headers:{
            'X-Oss-Force-Download': false
          },
          onHashProgress: function (progressData) {
            console.log ('校验中', JSON.stringify (progressData));
          },
           onProgress: function (progressData) {
            console.log ('上传中', JSON.stringify (progressData));
           },
       },
       function (err, data) {
         console.log('999',err,data)
        if (err) {
            message.error('文件上传失败,请重新上传')
            callback('')
        } else {
            let fileUrl = data.Location
            callback(fileUrl)
        }
        }
      )
  })
}

// 视频上传
function videoUpload(file,callbacks) {
  teachCommonStsToken2({}, 'post').then(response => { // 后台接口返回 密钥相关信息
    const data = response.data
    let startIndex = file.name.lastIndexOf('.')
    let urls = file.name.substring(startIndex);
    const [y, m, d] = [new Date().getFullYear(), String(new Date().getMonth() + 1).padStart(2, '0'), String(new Date().getDate()).padStart(2, '0')];
    let uploadFileName =  `video/console/${y}${m}${d}/${Date.now()}_${Math.random().toString(36).substr(2, 6)}${urls}` // Date.parse(new Date())
    var cos = new COS({
        getAuthorization: function(options, callback) {
          callback({
            TmpSecretId: data.credentials.tmpSecretId,
            TmpSecretKey: data.credentials.tmpSecretKey,
            XCosSecurityToken: data.credentials.sessionToken,
            StartTime: data.startTime,
            ExpiredTime: data.expiredTime,
            // expiration: data.expiration,
            requestId: data.requestId,
            ScopeLimit: true,
          })
        }
      })
      console.log(uploadFileName,data,file, file.name,urls, 'uploadFileName')
      cos.uploadFile({
          Bucket: data.bucket, // 存储桶名称
          Region: data.region, // 地区
          Key: uploadFileName, // 图片名称
          Body: file, // 上传文件对象
          Headers:{
            'X-Oss-Force-Download': false
          },
          onHashProgress: function (progressData) {
            console.log ('校验中', JSON.stringify (progressData));
          },
           onProgress: function (progressData) {
            console.log ('上传中', JSON.stringify (progressData));
           },
       },
       function (err, data) {
         console.log('999',err,data)
        if (err) {
            message.error('文件上传失败,请重新上传')
            callbacks('')
        } else {
            let fileUrl = data.Location
            callbacks(fileUrl)
        }
        }
      )
  })
}
export {upload, videoUpload};